﻿
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Web;

namespace GigaMatchTestEF
{
    public partial class GigaMatchTestEntities : DbContext
    {

        public override int SaveChanges()
        {
            
           // var ctx = ((IObjectContextAdapter)db).ObjectContext;

            foreach (var entry in this.ChangeTracker.Entries())
            {
                Type t = entry.Entity.GetType();


                if (entry.State ==  System.Data.EntityState.Added)
                {
                    var propCreatedDate = t.GetProperty("CreatedDate");
                    if (propCreatedDate != null)
                    {
                        propCreatedDate.SetValue(entry.Entity, DateTime.Now, null);
                    }


                    if (HttpContext.Current != null && HttpContext.Current.User != null)
                    {
                        string userName = HttpContext.Current.User.Identity.Name;

                        var propCreatedBy = t.GetProperty("CreatedBy");

                        if (propCreatedBy != null)
                        {
                            propCreatedBy.SetValue(entry.Entity, userName, null);
                        }
                    }
                    else if (Environment.UserName != null)
                    {
                        string userName = Environment.UserName;

                        var propCreatedBy = t.GetProperty("CreatedBy");

                        if (propCreatedBy != null)
                        {
                            propCreatedBy.SetValue(entry.Entity, userName, null);
                        }
                    }
                }
                else
                {
                    var propModifiedDate = t.GetProperty("ModifiedDate");

                    if (propModifiedDate != null)
                    {
                        propModifiedDate.SetValue(entry.Entity, DateTime.Now, null);
                    }


                    if (HttpContext.Current != null && HttpContext.Current.User != null)
                    {
                        string userName = HttpContext.Current.User.Identity.Name;

                        var propModifiedBy = t.GetProperty("ModifiedBy");

                        if (propModifiedBy != null)
                        {
                            propModifiedBy.SetValue(entry.Entity, userName, null);
                        }
                    }
                    else if (Environment.UserName != null)
                    {
                        string userName = Environment.UserName;

                        var propModifiedBy = t.GetProperty("ModifiedBy");

                        if (propModifiedBy != null)
                        {
                            propModifiedBy.SetValue(entry.Entity, userName, null);
                        }
                    }

                }

            }
           
            
            return base.SaveChanges();
        }
    }
}
